﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TimeTrackerAddin
{
    public partial class TimeTracker : Form
    {
        private String tfsServer = "sa12";
        private WorkItemStore workItemStore;
        private String[] WorkItemTypeStrings = { "Bug", "Risk", "Task", "Others" };
        private enum WorkItemCategory { Bug, Risk, Task, Others };

        public TimeTracker()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TeamFoundationServer teamFoundationServer = TeamFoundationServerFactory.GetServer(tfsServer);
            teamFoundationServer.Authenticate();
            workItemStore = new WorkItemStore(tfsServer);
            uri.Text = teamFoundationServer.Uri.ToString();
            un.Text = teamFoundationServer.AuthenticatedUserDisplayName + " " + teamFoundationServer.AuthenticatedUserName;

            ListAllProjects(workItemStore);
        }

        private void ListAllProjects(WorkItemStore workItemStore)
        {
            foreach (Project pr in workItemStore.Projects)
            {
                listProjects.Items.Add(pr);

            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listProjects_Click(object sender, EventArgs e)
        {
           
            Project project;
            project = (Project)listProjects.SelectedItem;

            try
            {

                WorkItemCollection workItemCollection = workItemStore.Query(
                 " SELECT [System.Id], [System.WorkItemType]," +
                 " [System.State], [System.AssignedTo], [System.Title] " +
                 " FROM WorkItems " +
                 " WHERE ([System.WorkItemType]=='Task' AND [System.TeamProject] = '" + project.Name +
                 "') ORDER BY [System.WorkItemType], [System.Id]");

                
                
                dgItems.AutoGenerateColumns = false;
                dgItems.DataSource = workItemCollection;
                //dgItems.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
        
                //foreach (WorkItem workItem in workItemCollection)
                //{
                    
                //    Field field = null;
                    
                    
                //    //if (workItem.Fields.Contains("Completed Work"))
                //    //{
                //    //    field = workItem.Fields["Completed Work"];

                //    //    if (field != null)
                //    //        listWorkItems.Items.Add(field.Name);

                //    //    //if (field.Value == null)
                //    //    //{
                //    //    //    workItem.Open();
                //    //    //    field.Value = 23;
                //    //    //    workItem.Save();
                //    //    //}
                //    //}


                //}
            }
            catch (Exception ex)
            {

            }
        }

    }
}
